package categories

import (
    "gohub/pkg/app"
    "gohub/pkg/database"
    "gohub/pkg/paginator"

    "github.com/gin-gonic/gin"
)

func Get(idstr string) (categories categories) {
    database.DB.Where("id", idstr).First(&categories)
    return
}

func GetBy(field, value string) (categories categories) {
    database.DB.Where("? = ?", field, value).First(&categories)
    return
}

func All() (categories []categories) {
    database.DB.Find(&categories)
    return 
}

func IsExist(field, value string) bool {
    var count int64
    database.DB.Model(categories{}).Where("? = ?", field, value).Count(&count)
    return count > 0
}
func Paginate(c *gin.Context, perPage int) (categories []categories, paging paginator.Paging) {
    paging = paginator.Paginate(
        c,
        database.DB.Model(categories{}),
        &categories,
        app.V1URL(database.TableName(&categories{})),
        perPage,
    )
    return
}